package com.dyj.ads

import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.{DataFrame, SparkSession}

object ads_e_mz_au5800_zdb {
  def main(args: Array[String]): Unit = {
    val ds: String = args(0)

    val sparkSession: SparkSession = SparkSession.builder()
      .appName("总蛋白指标统计")
      .enableHiveSupport()
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import sparkSession.implicits._
    import org.apache.spark.sql.functions._

    sparkSession.sql("use bigdata03_dws")

    val zdbDF: DataFrame = sparkSession.sql(
      s"""
        |select
        |baoGaoBianHao,
        |zongDanBai,
        |baoGaoRiQi,
        |zdb_max,
        |zdb_min,
        |zdb_avg
        |from
        |bigdata03_dws.dws_e_mz_au5800shenghua
        |where
        |ds='${ds}'
        |""".stripMargin)

    zdbDF
      .withColumn("number", count($"baoGaoBianHao") over Window.partitionBy($"baoGaoBianHao"))
      .withColumn("e_fc", ($"zongDanBai" - $"zdb_avg") * ($"zongDanBai" - $"zdb_avg"))
      .withColumn("a_fc", sum($"e_fc").over())
      .withColumn("zdb_fc", $"a_fc" / $"number")
      .withColumn("zdb_bzc", sqrt($"zdb_fc"))
      .withColumn("zdb_ycgs", sum(when($"zongDanBai" > 80 or ($"zongDanBai" < 60), 1)).over())
      .select($"baoGaoBianHao", $"zongDanBai", $"baoGaoRiQi", $"zdb_max", $"zdb_min", $"zdb_avg", $"zdb_fc", $"zdb_bzc", $"zdb_ycgs")
      .write
      .format("csv")
      .save("/daas/motl/bigdata03/ads/ads_e_mz_au5800_zdb/ds=" + ds)
  }

}
